package com.a4studio.android.newsreader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Properties;

import android.content.Context;
import android.util.Log;

public class Preferences {
	
	private final static String TAG=Preferences.class.getSimpleName();
	
	private Context context;
	
	public final static String PREF_FILE_NAME="prefs.ini";
	public final static String TIME_KEY="time";
	public final static String KEYWORD_KEY="keyword";
	public final static String RESULT_NUM_KEY="result";
	
	private Properties props;
	
	public Preferences( Context context)
	{
		this.context = context;		

		boolean isExist =true;
		try {
			context.openFileInput(PREF_FILE_NAME);
		} catch (FileNotFoundException e) {
			Log.e(TAG, "can't read pref file", e);
			isExist = false;
		}
		props = new Properties();
		if(!isExist)
		{
			reconstruct();
		}
		props.clear();
		
		FileInputStream fin;
		try {
			fin = context.openFileInput(PREF_FILE_NAME);
			props.load(fin);
			fin.close();
		} catch (FileNotFoundException e) {
			Log.e(TAG, "can't read pref file", e);
		} catch (IOException e) {
			Log.e(TAG, "can't read pref file", e);
		}
	}
	
	public int getResultNumSetting()
	{
		String time =  props.getProperty(RESULT_NUM_KEY, "1");
		int timeValue;
		try
		{
			timeValue = Integer.parseInt(time);
		}
		catch(Exception e)
		{
			//the file could be crashed, so reconstruct one more
			reconstruct();
			time =  props.getProperty(RESULT_NUM_KEY, "1");
			timeValue = Integer.parseInt(time);
		}
		
		return timeValue;
		
	}
	
	public long getTimeSetting()
	{
		String time =  props.getProperty(TIME_KEY, "0");
		long timeValue;
		try
		{
			timeValue = Long.parseLong(time);
		}
		catch(Exception e)
		{
			reconstruct();
			time =  props.getProperty(TIME_KEY, "0");
			timeValue = Long.parseLong(time);
		}
		
		return timeValue;
		
	}
	
	public String getKeywordSetting()
	{
		return props.getProperty(KEYWORD_KEY, "science");
	}
	
	public void setTimeSetting(long time)
	{
		try {
			Log.d(TAG, "setTimeSetting");
			FileOutputStream fos = context.openFileOutput(PREF_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
			props.setProperty(TIME_KEY, time+"");
			props.store(fos, "a4studio");
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't read pref file", e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't read pref file", e);
		}
	}
	
	public void setKeywordSetting(String keyword)
	{
		try {
			Log.d(TAG, "setKeywordSetting");
			FileOutputStream fos = context.openFileOutput(PREF_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
			props.setProperty(KEYWORD_KEY, keyword);
			props.store(fos, "a4studio");
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't read pref file", e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't read pref file", e);
		}
	}
	
	public void setResultNumSetting(int result)
	{
		try {
			Log.d(TAG, "setResultNumSetting");
			FileOutputStream fos = context.openFileOutput(PREF_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
			props.setProperty(RESULT_NUM_KEY, result+"");
			props.store(fos, "a4studio");
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't read pref file", e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't read pref file", e);
		}
	}
	
	private void reconstruct()
	{
		try {
			Log.d(TAG, "reconstruct");
			FileOutputStream fos = null;
			fos = context.openFileOutput(PREF_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
/*			props.setProperty(TIME_KEY, System.currentTimeMillis()+"");
			props.setProperty(KEYWORD_KEY, "science");
			props.store(fos, "reconstrut version");*/
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't create pref file", e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't create pref file", e);
		}
	}

}
